package org.example.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.netty.util.internal.StringUtil;
import org.example.backend.bean.UserBase;
import org.example.backend.common.LoginType;
import org.example.backend.common.Result;
import org.example.backend.common.constt.RedisConst;
import org.example.backend.dto.LoginDTO;
import org.example.backend.mapper.UserBaseMapper;
import org.example.backend.service.UserBaseService;
import org.example.backend.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 啊昌
 * @date 2025/7/19 11:47
 * UserBaseServiceImpl 用户实现类
 */
@Service
public class UserBaseServiceImpl implements UserBaseService{

	@Autowired
	private UserBaseMapper userBaseMapper;

	@Autowired
	private JwtUtils jwtUtils;


	@Autowired
	private RedisUtil redisUtil;


	/**
	 * 通过账号密码进行登录
	 *
	 * @param loginDTO 登录信息DTO
	 * @return 登录结果
	 */
	private Result<Map<String, Object>> loginByPassword(LoginDTO loginDTO){
		//账号密码登录
		Map<String, Object> result = new HashMap<>();
		//根据用户名去查询数据
		UserBase userBase = userBaseMapper.selectOne(new QueryWrapper<UserBase>().eq("username", loginDTO.getUsername()));
		boolean b = BCryptUtils.checkPassword(loginDTO.getPassword(), userBase.getPassword());
		if (!b) {
			//密码不匹配
			return Result.error("密码不匹配");
		}
		//todo 这里的token 生成还有问题密码校验通过 生成token
		String token = jwtUtils.generateToken(userBase.getUserId());
//			String token = jwtUtils.generateToken(userBase);
		result.put("token", token);
		return Result.success(result);
	}

	/**
	 * 通过邮箱登录
	 *
	 * @param loginDTO 登录信息对象
	 * @return 登录结果，包括用户信息和token
	 */
	private Result<Map<String, Object>> loginByMail(LoginDTO loginDTO) {
		String email = loginDTO.getEmail(); //邮箱
		String code = loginDTO.getCode(); //验证码
		Map<String, Object> result = new HashMap<>();
		//(StringUtil.isNullOrEmpty null -> true
		if((StringUtil.isNullOrEmpty(email) || StringUtil.isNullOrEmpty(code))){
			return Result.error("邮箱或验证码不能为空");
		}
		//查询用户信息
		UserBase userBase = userBaseMapper.selectOne(new QueryWrapper<UserBase>().eq("email", email));
		if (userBase == null) {
			//用户不存在
			return null;
		}
		//从缓存获取验证码
		String redis_code =(String)redisUtil.get(RedisConst.GetUserCaptcha(userBase.getUserId()));
		if(StringUtil.isNullOrEmpty(redis_code)){
			return Result.error("验证码不存在");
		}
		//校验验证码
		if(!code.equals(redis_code)){
			return Result.error("验证码不正确");
		}
		String token = jwtUtils.generateToken(userBase.getUserId());
		result.put("token", token);
		return Result.success(result);
	}

	@Override
	public Result<Map<String, Object>> login(LoginDTO loginDTO) {
		// 登录步骤 1. 校验参数 2.记录登录操作日志 3. 权限校验 4. 账号密码校验 5. 生成token
		Integer loginType = loginDTO.getLogin_type(); //登录类型
		Integer userType = loginDTO.getUserType(); //用户类型
		Integer status = loginDTO.getStatus(); //账号状态 状态：0-未验证，1-正常，2-封禁，3-注销

		if(status == 2){
			return Result.error("账号被封禁");
		}
		if(loginType == LoginType.ACCOUNT_PASSWORD){
			//账号密码登录
			return loginByPassword(loginDTO);
		}else if(loginType == LoginType.SMS){
			//短信登录
			return loginByMail(loginDTO);
		}else {
			//扫码登录
			Integer appScan = LoginType.APP_SCAN;
		}
		return null;
	}
}
